/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data;
    struct node* pNext;
} node_t;

node_t *addNode(node_t * head, int data)
{
    if (head == NULL) {
        node_t *tmp = (node_t *)malloc(sizeof(node_t));
        tmp->data = data;
        tmp->pNext = NULL;
        return tmp;
    }    
    
    node_t* pMove = head;
    while (pMove->pNext != NULL) {
        pMove = pMove->pNext;
    }
    
    pMove->pNext = (node_t *)malloc(sizeof(node_t));
    pMove->pNext->data = data;
    pMove->pNext->pNext = NULL;
    
    return head;
}

void printfNode(node_t * head)
{
    if (head == NULL) {
        printf("node is NULL\n");
    }
    
    node_t* pMove = head;
    while(pMove != NULL) {
        printf("data %d\n", pMove->data);
        pMove = pMove->pNext;
    }
}

node_t *revertNode(node_t * head)
{

    node_t *move_head = head;
    
    while(head->pNext != NULL) {

        node_t *move1 = head->pNext;
    
        head->pNext = move1->pNext;
        
        move1->pNext = move_head;
        move_head = move1;
    }
    
    return move_head;
}

int main()
{

    node_t * head  = addNode(NULL, 1);
    head  = addNode(head, 2);
    head  = addNode(head, 3);
    head  = addNode(head, 4);
    head  = addNode(head, 5);
    
    
    printfNode(head);

    printf("======================\n");
    head = revertNode(head);
    
    printfNode(head);

    return 0;
}
